package cn.hxz.controller;

import cn.hxz.pojo.Teacher;
import cn.hxz.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//教师controller
@Controller
//@RequestMapping("/t")
public class TeacherController {

    @Autowired
    TeacherService teacherService;


    @RequestMapping("/404")
    public String errorpage(Model model) {
        System.out.println("error....");
        return "404";
    }

    @RequestMapping(value = "/teacher", method = RequestMethod.GET)
    public String test() {
        System.out.println("start....");
        return "teacher";
    }

    //查询所有
    @GetMapping(value = "/selectAll")
    @ResponseBody
    public List<Teacher> selectAll(@RequestParam(value = "name") String name,
                                   @RequestParam(name = "pageIndex", defaultValue = "1", required = true) Integer pageIndex,
                                   @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize) {
        System.out.println("name=" + name);
        List<Teacher> teacherList = teacherService.selectAll(name,
                pageIndex, pageSize);
        return teacherList;
    }


    @PostMapping("/insert")
    public String insert(Teacher teacher) {
        System.out.println("insert:" + teacher);
        teacherService.insert(teacher);
        return "teacher";
    }

    @PostMapping("/update")
    public String update(@RequestBody Teacher teacher) {
        System.out.println(teacher);
        teacherService.update(teacher);
        return "teacher";
    }

    //redirect:selectAll
    @PostMapping("/delete")
    public String delete(Integer id) {
        System.out.println(id);
        teacherService.delete(id);
        return "teacher";
    }

    @GetMapping("/selectOne" )
    @ResponseBody
    public Teacher selectOne(@RequestParam("id") Integer id) {

        System.out.println("id=" + id);
        Teacher teacher = teacherService.selectOne(id);
        return teacher;
    }

}
